What is strip-bom?
The strip-bom package is used to remove a UTF-8 byte order mark (BOM) from the beginning of a string. This is particularly useful when dealing with files or data streams that may have been saved with a BOM, which can cause issues when processing or parsing the text.
What are strip-bom's main functionalities?
Removing BOM from a string
This code demonstrates how to use strip-bom to remove a byte order mark from the beginning of a string. It's useful for processing text that originates from files encoded with a BOM.
const stripBom = require('strip-bom');
const stringWithBom = '\uFEFFThis is a test string.';
const cleanedString = stripBom(stringWithBom);
console.log(cleanedString); // 'This is a test string.'
Other packages similar to strip-bom
strip-bom-stream
Similar to strip-bom, but designed for use with streams. It allows for the removal of the BOM from streams of data, making it useful for processing files or data in real-time. This package is a good choice when working with large files or in situations where data is being streamed.
strip-bom-buf
This package offers functionality similar to strip-bom but operates on buffers instead of strings. It's particularly useful when dealing with binary data or when reading files into buffers before processing. This can be advantageous for performance in certain scenarios.
strip-bom
Strip UTF-8 byte order mark (BOM) from a string/buffer/stream
From Wikipedia:
The Unicode Standard permits the BOM in UTF-8, but does not require nor recommend its use. Byte order has no meaning in UTF-8.
Install
$ npm install --save strip-bom
Usage
var fs = require('fs');
var stripBom = require('strip-bom');
stripBom('\ufeffUnicorn');
stripBom(fs.readFileSync('unicorn.txt'));
Or as a Transform stream:
var fs = require('fs');
var stripBom = require('strip-bom');
fs.createReadStream('unicorn.txt')
.pipe(stripBom.stream())
.pipe(fs.createWriteStream('unicorn.txt'));
CLI
$ npm install --global strip-bom
$ strip-bom --help
Usage
$ strip-bom <file> > <new-file>
$ cat <file> | strip-bom > <new-file>
Example
$ strip-bom unicorn.txt > unicorn-without-bom.txt
License
MIT © Sindre Sorhus